package com.fourth.service.impl;

import com.fourth.dao.UbCommentDao;
import com.fourth.dao.UbOrderDao;
import com.fourth.service.UbCommentService;
import com.fourth.service.UbOrderService;
import com.fourth.util.GetUidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.Comment;
import com.ljx.entity.EmpInfo;
import com.ljx.entity.Order;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/3/2 14:40
 **/
@Service
public class UbCommentServiceImpl implements UbCommentService {
    @Resource
    private UbCommentDao ubCommentDao;

    @Resource
    private UbOrderService ubOrderService;

    @Override
    public PageInfo<Comment> selectAll(Integer currentPage, Integer pageSize, String goodsid) {
        PageHelper.startPage(currentPage,pageSize);
        List<Comment> comments = ubCommentDao.findAll(goodsid);
        comments.stream().forEach(item -> {
            if (item.getImg() != null){
                String[] images = item.getImg().split(",,,");
                item.setImgs(images);
            }
        });
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        return commentPageInfo;
    }

    @Override
    public PageInfo<Comment> findMyComment(Integer currentPage, Integer pageSize, String goodsid) {
        PageHelper.startPage(currentPage,pageSize);
        String uid = GetUidUtil.getUid();
        List<Comment> comments = ubCommentDao.findMyComment(goodsid,uid);
        comments.stream().forEach(item -> {
            if (item.getImg() != null){
                String[] images = item.getImg().split(",,,");
                item.setImgs(images);
            }
        });
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        return commentPageInfo;
    }

    @Override
    public int insert(Comment comment) {
        comment.setCreatetime(new Date());

        List<Order> codes = ubOrderService.noCommentOrder(comment.getGoodsid());
        if (codes.size() < 0){
            return 0;
        }
        comment.setCode(codes.get(0).getCode());
        comment.setUserid(codes.get(0).getUid());
        comment.setStatus(0);
        comment.setIsDeleted(0);

        StringBuilder image = new StringBuilder();

        for (int i = 0; i < comment.getImgs().length; i++) {
            image.append(comment.getImgs()[i]);
            if (i < comment.getImgs().length - 1){
                image.append(",,,");
            }
        }
        String img = image.toString();
        comment.setImg(img);

        int insert = ubCommentDao.insert(comment);
        if (insert > 0){
            ubCommentDao.changeOrderCommentStatus(codes.get(0).getCode(),comment.getGoodsid());
        }
        return insert;
    }

    @Override
    public int updateComment(Comment comment) {
        comment.setUpdatetime(new Date());

        StringBuilder image = new StringBuilder();

        for (int i = 0; i < comment.getImgs().length; i++) {
            image.append(comment.getImgs()[i]);
            if (i < comment.getImgs().length - 1){
                image.append(",,,");
            }
        }
        String img = image.toString();
        comment.setImg(img);

        int update = ubCommentDao.updateById(comment);
        return update;
    }

    @Override
    public int deleteById(String id) {
        int delete = ubCommentDao.deleteById(id);
        return delete;
    }

    @Override
    public Comment getById(String id) {
        Comment comment = ubCommentDao.selectById(id);

        if (comment.getImg() != null){
            String[] images = comment.getImg().split(",,,");
            comment.setImgs(images);
        }
        return comment;
    }
}
